package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.service.JieyueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@CrossOrigin
public class Jieyuecontroller {
    @Autowired
    private JieyueService jieyueService;

    @PostMapping("/jieyue")
    @ResponseBody
    public Map gengxin(Book book,String id,String userid){
        Map map = new HashMap();
        int borrowing = jieyueService.jieyue(book);
            Jieyue jieyue1 = new Jieyue();
            jieyue1.setBorrowtime(new Date());
            jieyue1.setBook_id(id);
            jieyue1.setUser_id(userid);
        jieyueService.addjilu(jieyue1);
        map.put("code", 0);
        map.put("msg", "更新成功！");
        map.put("count", 1);
        map.put("data", borrowing);
        return map;
    }




    @GetMapping("/jieyuelist/{id}")
    @ResponseBody
    public MyResult jieyuelist(BookVO bookVO ,@PathVariable("id") Integer id) {
        if (bookVO.getPage() == 0) {
            bookVO.setPage(1);
        }
        int b = (bookVO.getPage() - 1) * bookVO.getLimit();
        bookVO.setB(b);
        Map<String, Object> map = new HashMap<>();
//        List<Book> getpage = jieyueService.jieyuepage(bookVO);
        List<BookVO> tanchu = jieyueService.geren(bookVO);

        Integer getcount = jieyueService.jieyuecount(bookVO);

        MyResult result = new MyResult(0,"注册成功！",getcount,tanchu);
        return result;
    }


    @GetMapping("jieyuelist")
    @ResponseBody
    public MyResult jieyuelist(BookVO bookVO) {
        if (bookVO.getPage() == 0) {
            bookVO.setPage(1);
        }
        int b = (bookVO.getPage() - 1) * bookVO.getLimit();
        bookVO.setB(b);
        Map<String, Object> map = new HashMap<>();
        List<Book> getpage = jieyueService.jieyuepage(bookVO);
        Integer getcount = jieyueService.jieyuecount(bookVO);

        MyResult result = new MyResult(0,"注册成功！",getcount,getpage);
//        map.put("code", 0);
//        map.put("msg", "注册成功！");
//        map.put("count", getcount);
//        map.put("data", getpage);
        return result;
    }


    @GetMapping("/jieyuelista")
    @ResponseBody
    public MyResult jieyuelista(BookVO bookVO) {
        if (bookVO.getPage() == 0) {
            bookVO.setPage(1);
        }
        int b = (bookVO.getPage() - 1) * bookVO.getLimit();
        bookVO.setB(b);
        Map<String, Object> map = new HashMap<>();
        List<Book> getpage = jieyueService.jieyuepage(bookVO);


        Integer getcount = jieyueService.jieyuecount(bookVO);

        MyResult result = new MyResult(0,"注册成功！",getcount,getpage);
//        map.put("code", 0);
//        map.put("msg", "注册成功！");
//        map.put("count", getcount);
//        map.put("data", getpage);
        return result;
    }


    //内容详情
    @PostMapping("/jieyuelist/{id}")
    @ResponseBody
    public Map<String, Object> tanchu(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        BookVO tanchu = jieyueService.xiangqing(id);
        map.put("code", 0);
        map.put("msg", "注册成功！");
        map.put("count", 1);
        map.put("data", tanchu);
        return map;
    }

    @PostMapping("/huanshu")
    @ResponseBody
    public Map huanshu(Book book,String id){
        Map map = new HashMap();
        int huan = jieyueService.huanshu(book);
        Jieyue jieyue1 = new Jieyue();
        jieyue1.setReturntime(new Date());
        jieyue1.setBorid(id);
        jieyueService.updatejilu(jieyue1);
        map.put("code", 0);
        map.put("msg", "更新成功！");
        map.put("count", 1);
        map.put("data", huan);
        return map;
    }
}
